home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
BORL_TIP
/
TI100
/
TI159.ASC
< prev
next >
Wrap
Text File
|
1991-09-11
|
6KB
|
265 lines
PRODUCT : TURBO PASCAL NUMBER : 159
VERSION : 2.0, 3.0
OS : PC-DOS, MS-DOS, CP/M-86
DATE : May 21, 1986 PAGE : 1/4
TITLE : REAL FORMAT CONVERSION
The following programs may be used to convert real number data
format from that used by Turbo Pascal with 8087 support, to the
format used by regular Turbo Pascal, and vice versa. The first
program converts from 8087 to regular, and the second program
converts from regular to 8087.
Program IEEErealTOregular;
{ Converts an 8-byte, IEEE real to Turbo Pascal's 6-byte real format. }
type
ieee = array[1..8] of byte;
turbo_real = array[1..6] of byte;
mant = array[1..5] of byte;
rf = file of ieee;
var
f : rf;
procedure IEEEtoTurbo(long : ieee; var r : real);
{ Convert from IEEE to Turbo Pascal's 6-byte real format }
var
i : integer;
e : byte;
t : turbo_real absolute r;
sign : byte;
begin
{ initialize variables }
r := 0.0;
for i := 2 to 5 do
t[i] := 0;
i := (long[8] and $7f) shl 4; { get 7 highest bits of exponent }
i := i or ((long[7] and $f0) shr 4);{ get rest of exponent }
if (i < 985) or (i > 1061) then { check to make sure exponent }
begin { is in legal range }
writeln('exponent too large');
exit;
end;
i := i - 1023; { take out bias }
t[1] := i + $81; { put in new bias }
PRODUCT : TURBO PASCAL NUMBER : 159
VERSION : 2.0, 3.0
OS : PC-DOS, MS-DOS, CP/M-86
DATE : May 21, 1986 PAGE : 2/4
TITLE : REAL FORMAT CONVERSION
sign := long[8] and $80; { get sign bit }
{ build up most significant byte }
t[6] := sign + ((long[7] and $0f) shl 3) or ((long[6] and $e0) shr 5);
{make rest of real number in groups of 5 and 3 }
for i := 5 downto 2 do
t[i] := ((long[i+1] and $1f) shl 3) or ((long[i] and $e0) shr 5);
end; { IEEEtoTurbo }
var
t : ieee;
r : real;
begin { program }
assign(f, 'real87.dat'); { open input file }
reset(f);
while not eof(f) do
begin
read(f, t); { read an 8-byte real }
ieeetoturbo(t, r); { convert to a 6-byte real }
writeln(r); { display results }
end;
end. { program }
program RegularTO87real;
{ Converts a 6-byte real to an 8-byte, IEEE real. }
type
ieee = array[1..8] of byte;
turbo_real = array[1..6] of byte;
rf = file of ieee;
var
f : rf;
i : file of real;
procedure TurboToIEEE(r : real; var long : ieee);
{ Converts from a 6-byte Turbo Pascal real number to an 8-
byte, IEEE format. }
PRODUCT : TURBO PASCAL NUMBER : 159
VERSION : 2.0, 3.0
OS : PC-DOS, MS-DOS, CP/M-86
DATE : May 21, 1986 PAGE : 3/4
TITLE : REAL FORMAT CONVERSION
var
realthing : turbo_real absolute r;
exp : integer;
sign, temp : byte;
i : integer;
begin
fillchar(long, sizeof(long), 0);
{ build up exponent and sign of long real }
sign := realthing[6] and $80; { get sign bit from 6-byte }
exp := realthing[1] - $81; { get exponent & take out bias}
exp := (exp + 1023) shl 4; { re-bias && left justify }
exp := exp or (sign shl 8); { put in sign }
long[8] := hi(exp); { insert into long real }
long[7] := lo(exp);
{ make mantissa }
for i := 6 downto 3 do
begin
{ build up a byte }
temp := (realthing[i] and $7f shl 1) or
(realthing[i - 1] and $80 shr 7);
{ split byte and insert into long real }
long[i+1] := long[i+1] + temp and $f0 shr 4;
long[i] := long[i] + temp and $f shl 4;
end;
{ take care of last (incomplete) byte }
temp := realthing[2] and $7f shl 1;
long[3] := long[3] + temp and $f0 shr 4;
long[2] := long[2] + temp and $f shl 4;
end; { TurboToIEEE}
var
t : ieee;
r : real;
PRODUCT : TURBO PASCAL NUMBER : 159
VERSION : 2.0, 3.0
OS : PC-DOS, MS-DOS, CP/M-86
DATE : May 21, 1986 PAGE : 4/4
TITLE : REAL FORMAT CONVERSION
fname : string[80];
begin { program }
write('input file name? '); { open input file }
readln(fname);
assign(i, fname);
reset(i);
fname := '';
write('output file name? '); { open output file }
readln(fname);
assign(f, fname);
rewrite(f);
while not eof(i) do
begin
read(i, r); { read a 6-byte real }
TurboToIEEE(r, t); { convert to an 8-byte real }
write(f, t); { output result to file }
end;
close(f);
end. {program} { close output file }